<?php

/**
 * @Author: king
 * @Date:   2019-02-20 19:16:04
 * @Last Modified by:   king
 * @Last Modified time: 2019-03-04 13:49:19
 */

namespace App\Tools\Validates;

use App\Tools\Validate;

/**
 * 档案相关校验
 * Class Astroset
 * @package App\Tools\Validate
 */
class Astroset extends Validate
{
   protected $field = [
        'id' => '星盘配置ID',
        'chart' => '星盘类型',
        'chart_value' => '星盘配置',
        'tid' => '配置方案'
    ];
    protected $rule = [
        'id' => 'require|integer|gt:0',
        'chart' => 'require|checkChart',
        'chart_value' => 'require|checkChartValue',
        'tid' => 'require|in:1,2,3'
    ];
    protected $scene = [
        'index' => ['tid', 'chart'],
        'update' => ['tid', 'chart', 'chart_value'],
        'delete' => ['tid', 'chart']
    ];

    protected $message = [
        'id.require'                    => '未知配置',
        'id.integer'                    => '未知配置',
        'id.gt'                         => '档案不存在',
        'chart.require'                 => '星盘类型未知1',
        'chart.checkChart'              => '星盘类型未知2',
        'chart_value.require'           => '星盘配置错误',
        'chart_value.checkChartValue'   => '星盘配置错误',
        'tid.require'                   => '配置方案错误',
        'tid.in'                        => '配置方案错误',
    ];

    /**
     * 检测是否包含当前星盘
     * @param $value
     * @param $rule
     * @param $data
     * @return bool
     */
    protected function checkChart($value, $rule, $data)
    {
        $astrosetConfig = config('astroset.chart');

        return false !== in_array($value, $astrosetConfig);
    }

    /**
     * 检测客户端传递的星盘配置参数是否合法
     * @param $value
     * @param $rule
     * @param $data
     * @return bool
     */
    protected function checkChartValue($value, $rule, $data)
    {
        $result = true;

        $chartValue = json_decode($value, true);

        if (!isset($chartValue['planet_orb_all']) || count($chartValue['planet_orb_all']) != 10) {
            $result = false;
        }

        if ($result === true && (!isset($chartValue['h_sys']) || strlen($chartValue['h_sys']) != 1)) {
            $result = false;
        }

        if ($result === true && (!isset($chartValue['planets_display']) || strlen($chartValue['planets_display']) != 21)) {
            $result = false;
        }

        if ($result === true && (!isset($chartValue['asp_orb_all']) || count($chartValue['asp_orb_all']) != 11)) {
            $result = false;
        }

        if ($result === true && (!isset($chartValue['asps_display']) || count($chartValue['asps_display']) > 11)) {
            $result = false;
        }

        return $result;
    }
}
